class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None

def build_tree(preorder, inorder):
    if not inorder or not preorder:
        return None
    
    root = TreeNode(preorder[0])
    mid = inorder.index(preorder[0])
    root.left = build_tree(preorder[1:1+mid], inorder[:mid])
    root.right = build_tree(preorder[mid+1:], inorder[mid+1:])
    return root

def update_tree(node):
    if not node:
        return 0
    left_sum = update_tree(node.left)
    right_sum = update_tree(node.right)
    old_val = node.val
    node.val = left_sum + right_sum
    return node.val + old_val

def inorder_travel(node):
    if not node:
        return []
    return inorder_travel(node.left) + [node.val] + inorder_travel(node.right)
    
        
def main():
    inorder = list(map(int, input().split()))
    preorder = list(map(int, input().split()))
    root = build_tree(preorder, inorder)
    update_tree(root)
    print(' '.join(map(str, inorder_travel(root))))
    
if __name__ == "__main__":
    main()